/*
 * @Description: 
 * @Note: 
 * @Author: ComWinTop
 * @Date: 2021-03-06 11:50:06
 * @LastEditors     : lzg
 * @LastEditTime    : 2022-12-12 20:57:36
 */

//串口工具
var PageComTool = {
    id: "PageComTool", //页面id
    menu: "i18n.lan.menu.other", //所属菜单(一级菜单名)
    title: "i18n.lan.menu.PageComTool", //标题（二级菜单名）
    icon: "icon-me-com", //图标
    autoOpen: true, //自动打开标签
    closable: false, //不可关闭
    init: function () {
        setTimeout(function () {
            eval("receivers").push(pageComTool_dataReceived); //添加接收串口数据的函数
            eval("lineReceivers").push(pageComTool_newLineReceived); //添加接收行数据的函数
        }, 3000);
    },
    el: [ //控件
        { //样式
            elname: "style",
            text: ""
        },
        { //接收文字
            elname: "label",
            text: i18n.lan.PageComTool.receive,
            endRow: true,
        },
        { //接收控件
            elname: "textarea",
            attrs: {
                style: "width:100%;height:280px;resize:none;outline:none;rows:10;",
                id: "pageComTool_recvTA",
                readonly: "readonly"
            },
            endRow: true,
        },
        { //发送文字
            elname: "label",
            text: i18n.lan.PageComTool.send,
        },
        { //清除按钮
            elname: "button",
            text: i18n.lan.PageComTool.clear,
            attrs: {
                onclick: "document.getElementById('pageComTool_recvTA').innerHTML='';",
                style: "float:right;margin-top:-5px;",
            },
        },
        { //自动滚动
            elname: "checkbox",
            label: i18n.lan.PageComTool.autoScroll,
            id: "pageComTool_cbAutoScroll",
            attrs: {
                checked: true,
                style: "margin-left:70%;"
            },
            endRow: true,
        },
        { //发送控件
            elname: "textarea",
            attrs: {
                style: "width:100%;height:100px;resize:horizontal;",
                id: "pageComTool_sendTA",
            },
            endRow: true,
        },
        { //自动添加%和回车控制
            elname: "checkbox",
            label: i18n.lan.PageComTool.autoAddHeadAndEnd,
            id: "pageComTool_cbAutoAddHeadAndEnd",
            attrs: {
                checked: true,
                title: "VER=>%VER\\n;but 000000=>000000\\n",
            },
        },
        { //串口调试开关
            elname: "checkbox",
            label: i18n.lan.PageComTool.uartevent,
            id: "pageComTool_cbUartevent",
            attrs: {
                style: "margin-left:20px;",
                onchange: "sendCmd('UARTEVENT'+(this.checked?'1':'0'));",
                dumpKey: "SYS_UART_EVENT_ENABLED",
            },
        },
        { //发送按钮
            elname: "button",
            text: i18n.lan.common.send,
            attrs: {
                onclick: "PageComTool_SendData();",
                style: "float:right;",
            },
        },
        { //清空
            elname: "button",
            text: i18n.lan.PageComTool.clear,
            attrs: {
                onclick: "$('#pageComTool_sendTA')[0].innerHTML = ''",
                style: "float:right;margin-right:30px",
            },
        },
        { //查询时间
            elname: "button",
            text: i18n.lan.PageComTool.queryTime,
            endRow: true,
            attrs: {
                onclick: "sendCmd('SYSTIME')",
                style: "float:right;margin-right:30px",
            },
        },
    ],
}
CmdPage.add(PageComTool); //添加到页面管理

// 接收区控件
// var PageComTool_taRecv = $("#pageComTool_recvTA");
// var pageComTool_cbAutoScroll = $("#pageComTool_cbAutoScroll");
var count = 0;
//接收到串口数据
function pageComTool_dataReceived(msg) {
    //RTU刷新指令需要换行显示才比较好阅读
    msg = msg.replace(/\r\n/g, "\n").replace(/\r/g, "\n"); //将\r换为\n
    // if (typeof (PageComTool_taRecv) != "undefined") { //对象存在
    PageComTool_taRecv = $("#pageComTool_recvTA");
    pageComTool_cbAutoScroll = $("#pageComTool_cbAutoScroll");
    if (pageComTool_cbAutoScroll[0].checked && count++ > 1000) {
        PageComTool_taRecv[0].innerHTML = '';
        count = 0;
    }
    // }
    // 显示数据
    PageComTool_taRecv.append(msg);
    if (pageComTool_cbAutoScroll[0]) {
        scrollToTextareaEnd(PageComTool_taRecv[0], pageComTool_cbAutoScroll[0].checked);
    }
    if (dumped) { //刷新中
        progressBar.val((progressBar.val() + 1) % 80);
    }
}
//接收到行数据
function pageComTool_newLineReceived(line) { }

//发送数据
function PageComTool_SendData() {
    let txt = $('#pageComTool_sendTA').val();
    if (txt.startsWith("js:")) {
        txt = txt.replace("js:", "");
        eval(txt);
        return;
    } else if ($("#pageComTool_cbAutoAddHeadAndEnd")[0].checked) {
        if (txt != "000000")
            txt = Device.addCmdHE(txt);
        else
            txt += "\n";
    }
    sendData(txt);
}